package com.rop.znyx.common.exception;

import com.rop.znyx.common.result.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @Title: GlobalExceptionHandler
 * @Author Rop
 * @Date 2023/6/7 21:23
 * @description: 全局异常处理器
 */

@Slf4j
@ControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(Exception.class)
    @ResponseBody
    public Result<Void> error(Exception e) {
        log.error("===>全局异常处理<===", e);
        return Result.fail();
    }

    /**
     * 自定义异常处理方法
     */
    @ExceptionHandler(BizException.class)
    @ResponseBody
    public Result<Void> error(BizException e) {
        return Result.build(null, e.getCode(), e.getMessage());
    }
}
